package com.hctforgreen.greenservice;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.hctforgreen.greenservice.ui.adapter.FeedBackListAdapter;
import com.hctforgreen.greenservice.ui.handler.FeedBackListHandler;
import com.hctforgreen.greenservice.utils.LoginResultStoreUtil;
import com.hctforgreen.greenservice.utils.ThemeStoreUtil;
import com.umeng.analytics.MobclickAgent;

/**
 * 信息反馈板块
 * 
 * @author Tallen Zhai
 * 
 */
public class FeedbackActivity extends ParentActivity implements OnClickListener {
	private boolean has_contact = false;
	private static final int REQUEST_CODE = 10000;
	private String dialPhone;// 拨打电话号码

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_feedback_v2);

		initSkinLayout();

		initTitleButtonBar();
		initWindow();
	}

	private void initTitleButtonBar() {
		final TextView titleTv = (TextView) findViewById(R.id.tv_title);
		titleTv.setText(getString(R.string.feed_back_title_hint));

		final ImageButton backBtn = (ImageButton) findViewById(R.id.btn_back);
		backBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				finish();
			}
		});
		final TextView persionNameTv = (TextView) findViewById(R.id.tv_persion_name);
		persionNameTv
				.setText(LoginResultStoreUtil.get(FeedbackActivity.this).personName);

		final Button callBtn = (Button) findViewById(R.id.btn_call);
		callBtn.setVisibility(View.INVISIBLE);
		callBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (!(handler == null || handler.getDialPhone() == null || ""
						.equals(handler.getDialPhone().trim()))) {

					has_contact = true;
					MobclickAgent.onEvent(FeedbackActivity.this,
							"Account_dial_phone");
					Intent intent = new Intent(Intent.ACTION_CALL, Uri
							.parse("tel:" + handler.getDialPhone()));
					startActivity(intent);
				}
			}
		});
		final Button addFeedbtn = (Button) findViewById(R.id.btn_add_feed);
		addFeedbtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				MobclickAgent.onEvent(FeedbackActivity.this, "Write_feadback");
				Intent intent = new Intent();
				intent.setClass(FeedbackActivity.this,
						SubmitFeedBackActivityV3.class);
				startActivityForResult(intent, REQUEST_CODE);
			}
		});
	}

	public void initWindow() {
		findViewById(R.id.lyt_my_feedback).setOnClickListener(this);
		findViewById(R.id.lyt_open_case).setOnClickListener(this);
		findViewById(R.id.lyt_draft).setOnClickListener(this);

		// getFeedBackJsonByPersonId(LoginResultStoreUtil.get(this).personId);
	}

	private FeedBackListHandler handler;

	private void getFeedBackJsonByPersonId(String personId) {
		View convertView = findViewById(R.id.lyt_parent);
		ListView listView = (ListView) convertView
				.findViewById(R.id.lst_default_list);
		handler = new FeedBackListHandler(this, convertView, personId,
				dialPhone, false);
		new FeedBackListAdapter(listView, this, convertView,
				R.layout.layout_loading, R.layout.layout_reloading, handler);
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (has_contact) {
			has_contact = !has_contact;
			Intent intent = new Intent();
			intent.setClass(FeedbackActivity.this,
					SubmitPhoneFeedBackActivity.class);
			startActivity(intent);
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode != RESULT_OK)
			return;
		if (requestCode == REQUEST_CODE) {
			if (handler != null) {
				handler.refersh();
			}
		}
	}

	@Override
	protected void onDestroy() {

		super.onDestroy();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.lyt_my_feedback:// 我的反馈
			Intent intent = new Intent(this, MyFeedbackActivity.class);
			startActivity(intent);
			break;
		case R.id.lyt_open_case:// 公开案例
			intent = new Intent(this, OpenFeedbackActivity.class);
			startActivity(intent);
			break;
		case R.id.lyt_draft:// 草稿箱
			intent = new Intent(this, FeedbackHistoryActivity.class);
			startActivity(intent);
			break;
		}

	}
}
